package com.company.huisuo.biz.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import com.company.huisuo.base.BaseSingleAdapter
import com.company.huisuo.biz.dialog.ArtistRoleTipsDialog
import com.company.huisuo.databinding.ItemArtistClothesBinding
import com.lxj.xpopup.XPopup


class ArtistClothesAdapter : BaseSingleAdapter<Pair<String, String>, ItemArtistClothesBinding>() {

    private var mSelectPos = -1

    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemArtistClothesBinding
        get() = ItemArtistClothesBinding::inflate

    override fun convert(
        holder: SingleViewHolder<ItemArtistClothesBinding>,
        position: Int,
        item: Pair<String, String>?
    ) {
        holder.binding.tvCheck.apply {
            text = item?.first ?: ""
            isSelected = mSelectPos == position
            setOnClickListener {
                if (item?.second?.isEmpty() == false) {
                    //弹窗
                    XPopup.Builder(context)
                        .atView(holder.binding.tvCheck)
                        .watchView(holder.itemView)
                        .hasShadowBg(false)
                        .asCustom(ArtistRoleTipsDialog(context as Activity, item.second))
                        .show()
                }

                val oldPos = mSelectPos
                mSelectPos = position
                // 使用局部刷新，避免整个列表重新绑定
                if (oldPos != -1) {
                    notifyItemChanged(oldPos)
                }
                notifyItemChanged(position)
            }
        }
    }
}